package cn.edu.zzuli.nothinglink.controller;


import cn.edu.zzuli.nothinglink.anno.CurrentUser;
import cn.edu.zzuli.nothinglink.entity.R;
import cn.edu.zzuli.nothinglink.service.SignService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suzyyy
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/sign")
public class SignController {

    @Autowired
    SignService signService;

    //发布签到
    @PostMapping("/pub/{stream}")
    public R publishSign(@PathVariable String stream, @RequestBody JSONObject jsonObject) {
        if (jsonObject.getInteger("signTimeLimit") != null
                //
                && !stream.equals("undefined")) {

            if (signService.pushSign2AllStuClients(stream, jsonObject.getInteger("signTimeLimit"))) {
                return R.success();
            }
            return R.error().add("error", "上次签到还没结束呢 : )");
        }
        return R.error().add("error", "参数问题, 请传入限制时间和正确的stream");
    }

    @GetMapping("/condition/my")
    public R getSignInfo(@CurrentUser Integer teaId) {
        if (teaId == null) return R.error().add("error", "请先登陆");
        return signService.getSignCondition(teaId);
    }

}

